//
//  HWSaveMoneyCardController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/6/29.
//  Copyright © 2022 94hwan. All rights reserved.
//

import UIKit

class HWSaveMoneyCardController: HWBaseViewController {
    var topBgView:UIView!
    var bottomBgView:UIView!
    var tableView:UITableView!
    let tableViewProtocol = HWSaveMoneyCardProtocol()
    var statusBarStyle:UIStatusBarStyle = .default
    let httpManager = HWSaveMoneyCardHttpManager()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        addAction()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.statusBarStyle = .lightContent
        setNeedsStatusBarAppearanceUpdate()
        loadData()
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        let _ = super.preferredStatusBarStyle
        return self.statusBarStyle
    }

}

extension HWSaveMoneyCardController {
    private func initUI() -> Void {
        navigationView.titleLabel.text = "省钱卡"
        navigationView.titleLabel.textColor = .white
        navigationView.backButton.setImage(UIImage(named: "whiteNavBack"), for: .normal)
        
        topBgView = UIView()
        topBgView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 361*widthScale)
        view.addSubview(topBgView)
        UIColor.gradientColor(view: topBgView, colors: [UIColor.colorWithAlphaHexString(hexString: "#333333", alpha: 0.92).cgColor,
                                                        UIColor.colorWithHexString(hexString: "#1E1E1E").cgColor], start: CGPoint(x: 0, y: 1), end: CGPoint(x: 0, y: 0))
        
        bottomBgView = UIView()
        bottomBgView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 769*widthScale)
        bottomBgView.backgroundColor = .colorWithHexString(hexString: "#272727")
        view.addSubview(bottomBgView)
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never

        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -120*widthScale
        
        view.bringSubviewToFront(navigationView)
    }
    
    private func initFrame() -> Void {
        topBgView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(361*widthScale)
        }
        
        bottomBgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(250*widthScale)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
        }
    }
}

extension HWSaveMoneyCardController {
    func loadData() -> Void {
        httpManager.getSaveMoneyCardInfo { model in
            if model.is_buy == "1" {
                model.selectZhou = true
            } else if model.is_buy == "2" {
                model.selectZhou = false
            }
            self.tableViewProtocol.model = model
            self.tableView.reloadData()
        } failure: { errorString in
            
        }

    }
}

extension HWSaveMoneyCardController {
    func addAction() -> Void {
        self.tableViewProtocol.selectButtonClick = {
            self.tableView.reloadData()
        }
        
        self.tableViewProtocol.payButtonClick = {
            type ,id in
            self.httpManager.getSaveMoneyCardOrder(type: type, sqk_id: id) { model in
                let web = XMGWebViewController()
                web.url = URL(string: model.url)!
                self.navigationController?.pushViewController(web, animated: true)
            } failure: { errorString in
                self.view.showHUD(title: errorString)
            }

        }                                              
        
        self.tableViewProtocol.bottomButtonClick = {
            id,day in
            self.httpManager.getSaveMoneyReceiveCoupon(id: id, day: day) { message in
                self.view.showHUD(title: message)
                self.loadData()
            } failure: { errorString in
                self.view.showHUD(title: errorString)
            }

        }
    }
}
